package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    //定义Mapper
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> findList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }
    //提供setter方法用于注入属性

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int row = 0;
        for (int i = 0; i <sysUserList.size() ; i++) {
            //拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if(i == 1){
                //插入第二条时,模拟数据库异常,查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            //调用插入数据操作,记录总受影响条数
            row += sysUserMapper.insert(sysUser);
        }
        return row;
    }

    @Override
    public int update(SysUser sysUser) {
        return sysUserMapper.update(sysUser);
    }

    @Override
    public int delete(Long id) {
        return sysUserMapper.delete(id);
    }
}
